package com.base.cn.platform.os.controller.qa.qatag;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.qa.qatag.QaTag;
import com.base.cn.platform.os.service.qa.qatag.QaTagBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 问答标签Controller
 * @author LIZHIPENG
 * @since 2018/4/17
 */
@RestController
@RequestMapping("/course")
public class QaTagController extends BaseController {

    @Autowired
    private QaTagBiz qaTagBiz;

    /**
     * 查询标签数量
     * @return
     */
    @RequestMapping("/tagCount")
    public BigDecimal count(){
        return qaTagBiz.count("status != 3");
    }

    /**
     * 分页查询标签列表
     * @param currentPage
     * @param qaTag
     * @return
     */
    @RequestMapping("/queryQaTagListPage")
    public PageInfo<QaTag> queryQaTagListPage(@RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                          @RequestBody QaTag qaTag){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        PageInfo<QaTag> qaTagPageInfo = qaTagBiz.queryQaTagListPage(page, qaTag);
        return qaTagPageInfo;
    }

    /**
     * 查询标签列表(不分页)
     * @param qaTag 查询条件
     * @return
     */
    @RequestMapping("/queryQaTagList")
    public List<QaTag> queryQaTagList(@RequestBody QaTag qaTag){
        return qaTagBiz.queryQaTagList(qaTag);
    }

    /**
     * 保存问答标签
     * @param qaTag
     * @return
     */
    @RequestMapping("/saveQaTag")
    public Map<String, Object> saveQaTag(@RequestBody QaTag qaTag){
        if(ObjectUtils.isEmpty(qaTag)){
            return ResultUtil.ERROR("标签实体为空，系统异常");
        }
        if(StringUtils.isEmpty(qaTag.getTagName()) || StringUtils.isEmpty(qaTag.getTagName().trim())){
            return ResultUtil.ERROR("标签名称不能为空");
        }
        return qaTagBiz.saveQaTag(qaTag);
    }

    /**
     * 批量修改问答标签状态
     * @param tagIds
     * @param status
     * @return
     */
    @RequestMapping("/updateQaTag")
    public Map<String, Object> updateQaTag(@RequestParam("tagIds")String tagIds,
                                           @RequestParam("status") int status){
        if(StringUtils.isEmpty(tagIds) || StringUtils.isEmpty(tagIds.trim())){
            return ResultUtil.ERROR("标签ID串为空");
        }
        QaTag qaTag = new QaTag();
        qaTag.setStatus(status);
        qaTagBiz.updateByWhereSql(qaTag, "id in (" + tagIds + ")");
        return ResultUtil.SUCCESS("标签删除成功");
    }
}
